package com.xianniu.ltc.controller;

import cn.devezhao.commons.web.ServletUtils;
import cn.devezhao.commons.web.WebUtils;
import cn.devezhao.persist4j.engine.ID;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.poi.excel.ExcelUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONAware;
import com.alibaba.fastjson.JSONObject;
import com.rebuild.api.RespBody;
import com.rebuild.core.Application;
import com.rebuild.core.UserContextHolder;
import com.rebuild.core.configuration.general.DataListManager;
import com.rebuild.core.privileges.bizz.ZeroEntry;
import com.rebuild.core.service.dataimport.DataExporter;
import com.rebuild.core.support.general.BatchOperatorQuery;
import com.rebuild.utils.ExcelUtils;
import com.rebuild.utils.JSONUtils;
import com.rebuild.web.EntityController;
import com.rebuild.web.user.signup.LoginController;
import com.xianniu.ltc.enums.*;
import com.xianniu.ltc.model.po.*;
import com.xianniu.ltc.model.po.imports.ImportLeadCountPO;
import com.xianniu.ltc.model.po.imports.ImportLeadPO;
import com.xianniu.ltc.model.vo.XnLeadVO;
import com.xianniu.ltc.service.XnLeadService;
import com.xxl.sso.core.entity.ReturnT;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author WangRuiJie
 * @Title: XnLeadController
 * @Package com.xianniu.ltc.controller
 * @Description: 线索管理controller
 * @date 2021/6/911:22 上午
 */
@RestController
@Slf4j
@RequestMapping(value = "/XnLead")
public class XnLeadController extends EntityController {

    // 限制导入最长行数
    private final Integer MAX_USER_IMPORT = 20000;
    @Autowired
    private XnLeadService xnLeadService;

    /*************************************************************** 后台开始 ***************************************************************/
    /**
     * 后台查询线索列表
     * @return
     */
    @GetMapping("/selectLeadForAdmin")
    public ModelAndView selectLeadForAdmin(HttpServletRequest request) {
        String tenantCode = (String) ServletUtils.getSessionAttribute(request, LoginController.TENANT_CODE);
        UserContextHolder.setTenant(tenantCode);
        // TODO 未确定前端HTML
        ModelAndView mv = createModelAndView("/xianniu/admin/lead/lead-list");
        mv.getModel().put("entityName", "XnLead");
        mv.getModel().put("entityLabel", "线索管理");
        mv.getModel().put("entityIcon", "entityIcon");
        JSON config = buildLeadAdminDataListConfig();
        mv.getModel().put("DataListConfig", JSON.toJSONString(config));
        return mv;
    }

    /**
     * 新前端调用返回JSON：后台查询线索列表
     * @return
     */
    @GetMapping("/selectLeadForAdmin-new")
    public JSONAware selectLeadForAdminNew(HttpServletRequest request) {
        String tenantCode = (String) ServletUtils.getSessionAttribute(request, LoginController.TENANT_CODE);
        UserContextHolder.setTenant(tenantCode);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("entityName", "XnLead");
        jsonObject.put("entityLabel", "线索管理");
        jsonObject.put("entityIcon", "entityIcon");
        JSON config = buildLeadAdminDataListConfig();
        jsonObject.put("DataListConfig", JSON.toJSONString(config));
        return jsonObject;
    }

    /**
     * 根据ID查询线索详情
     * @param id
     * @return
     */
    @GetMapping("/detailLead")
    public ModelAndView detailLeadForAdmin(String id, HttpServletRequest request) {
        String tenantCode = (String) ServletUtils.getSessionAttribute(request, LoginController.TENANT_CODE);
        UserContextHolder.setTenant(tenantCode);
        // TODO 未确定前端HTML
        ModelAndView mv = createModelAndView("/xianniu/admin/lead/lead-view");
        log.info("后台-根据ID查询线索详情:{}", JSON.toJSON(id));
        XnLeadVO vo = xnLeadService.detailLeadForAdmin(id);
        mv.getModel().put("leadVo", vo);
        return mv;
    }

    /**
     * 新前端调用返回JSON：线索详情
     * @return
     */
    @GetMapping("/detailLead-new")
    public JSONAware detailLeadForAdminNew(String id, HttpServletRequest request) {
        String tenantCode = (String) ServletUtils.getSessionAttribute(request, LoginController.TENANT_CODE);
        UserContextHolder.setTenant(tenantCode);
        JSONObject jsonObject = new JSONObject();
        log.info("后台-根据ID查询线索详情:{}", JSON.toJSON(id));
        XnLeadVO vo = xnLeadService.detailLeadForAdmin(id);
        jsonObject.put("leadVo", vo);
        return jsonObject;
    }

    /**
     * 后台-单个/批量转移分组
     * @param po
     * @return
     */
    @PostMapping("/transferLeadForAdmin")
    public JSONAware transferLeadForAdmin(@RequestBody TransferLeadPO po, HttpServletRequest request) {
        if (po.getLeadIdList().isEmpty()){
            return RespBody.errorl("PlsSet1FieldShowLeast");
        }
        String tenantCode = (String) ServletUtils.getSessionAttribute(request, LoginController.TENANT_CODE);
        UserContextHolder.setTenant(tenantCode);
        log.info("后台-单个/批量转移分组:{}", JSON.toJSON(po));
        ReturnT returnT = xnLeadService.transferLeadForAdmin(po);
        if (returnT.getCode() != 200){
            return RespBody.errorl(returnT.getMsg());
        }
        return RespBody.ok();
    }


    /**
     * 后台-单个/删除线索
     * @param idList
     * @return
     */
    @PostMapping("/deleteLeadForAdmin")
    public JSONAware deleteLeadForAdmin(@RequestParam List<String> idList, HttpServletRequest request) {
        if (idList.isEmpty()){
            return RespBody.errorl("PlsSet1FieldShowLeast");
        }
        String tenantCode = (String) ServletUtils.getSessionAttribute(request, LoginController.TENANT_CODE);
        UserContextHolder.setTenant(tenantCode);
        log.info("后台-单个/删除线索:{}", JSON.toJSON(idList));
        ReturnT returnT = xnLeadService.deleteLeadForAdmin(idList);
        if (returnT.getCode() != 200){
            return RespBody.errorl(returnT.getMsg());
        }
        return RespBody.ok();
    }

    /**
     * 后台：回收站查询线索列表
     * @return
     */
    @GetMapping("/selectLeadFallbackForAdmin")
    public ModelAndView selectLeadFallbackForAdmin(HttpServletRequest request) {
        final ID user = getRequestUser(request);
        String tenantCode = (String) ServletUtils.getSessionAttribute(request, LoginController.TENANT_CODE);
        UserContextHolder.setTenant(tenantCode);
        ModelAndView mv = createModelAndView("/xianniu/admin/lead/lead-fallback");
        mv.getModel().put("entityName", "XnLead");
        mv.getModel().put("entityLabel", "线索回收站");
        mv.getModel().put("entityIcon", "entityIcon");
        JSON config = buildLeadAdminDataListConfig();
        mv.getModel().put("DataListConfig", JSON.toJSONString(config));
        return mv;
    }

    /**
     * 新前端调用返回JSON：回收站查询线索列表
     * @return
     */
    @GetMapping("/selectLeadFallbackForAdmin-new")
    public JSONAware selectLeadFallbackForAdminNew(HttpServletRequest request) {
        String tenantCode = (String) ServletUtils.getSessionAttribute(request, LoginController.TENANT_CODE);
        UserContextHolder.setTenant(tenantCode);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("entityName", "XnLead");
        jsonObject.put("entityLabel", "线索回收站");
        jsonObject.put("entityIcon", "entityIcon");
        JSON config = buildLeadAdminDataListConfig();
        jsonObject.put("DataListConfig", JSON.toJSONString(config));
        return jsonObject;
    }

    /**
     * 后台-单个/批量还原
     * @param idList
     * @return
     */
    @PostMapping("/resetLeadForAdmin")
    public JSONAware resetLeadForAdmin(@RequestParam List<String> idList, HttpServletRequest request) {
        if (idList.isEmpty()){
            return RespBody.errorl("PlsSet1FieldShowLeast");
        }
        String tenantCode = (String) ServletUtils.getSessionAttribute(request, LoginController.TENANT_CODE);
        UserContextHolder.setTenant(tenantCode);
        log.info("后台-单个/批量还原:{}", JSON.toJSON(idList));
        ReturnT returnT = xnLeadService.resetLeadForAdmin(idList);
        if (returnT.getCode() != 200){
            return RespBody.errorl(returnT.getMsg());
        }
        return RespBody.ok();
    }

    /**
     * 功能描述: 下载线索导入模板
     *
     * @Param: [response, request]
     * @Return: void
     * @Author: wrj
     * @Date: 2022/1/21 5:35 下午
     */
    @RequestMapping(value = "/downloadTemplate")
    @ResponseBody
    public void downloadSortImport(HttpServletResponse response){
        String fileName = "导入线索管理模板";
        String sheetName = "导入线索管理模板";
        List<ImportLeadPO> userList = new ArrayList<>();
        ImportLeadPO po = new ImportLeadPO();
        po.setLeadName("线索名称必填");
        po.setXiansuolaiyuan("企业微站/企业官网/展会/名片/会议营销/百度/其他");
        po.setZhongyaochengdu("一级/二级/三级/四级/五级");
        po.setGenjinzhuangtai("已关闭/已联系/待处理");
        po.setArea("朝阳区");
        po.setIndustry("其他/制造业/咨询/娱乐/媒体/政府/教育/服务/电信/能源/金融/零售/非盈利事业/高科技");
        userList.add(po);
        try {
            ExcelUtils.writeExcelTemplate(response, userList, fileName, sheetName, ImportLeadPO.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 导入：同步读，单sheet
     * 注意：这里为了介绍 excel导入导出，代码都写在 controller，实际项目开发中，校验和处理代码建议放到 service
     */
    @PostMapping("/excel/import")
    public ReturnT importData(HttpServletRequest request) {
        CommonsMultipartResolver resolver = new CommonsMultipartResolver(request.getServletContext());
        MultipartFile fileA = null;
        MultipartHttpServletRequest mp = resolver.resolveMultipart(request);
        for (MultipartFile t : mp.getFileMap().values()) {
            // 只允许一个
            fileA = t;
            break;
        }
        if (fileA == null || fileA.isEmpty()) {
            return new ReturnT(400, "文件不存在");
        }
        String tenantCode = (String) ServletUtils.getSessionAttribute(request, LoginController.TENANT_CODE);
        UserContextHolder.setTenant(tenantCode);
        Integer successCount=0;
        ImportLeadCountPO countPO = new ImportLeadCountPO();
        // 1.excel同步读取数据
        try {
            List<ImportLeadPO> excelList = EasyExcel.read(new BufferedInputStream(fileA.getInputStream())).head(ImportLeadPO.class).sheet().doReadSync();
            // 2.检查是否大于20000条
            if (excelList.size() > MAX_USER_IMPORT) {
                return new ReturnT(400, "数据不能超过20000行");
            }
            // 3.导入校验所有行列格式
            countPO = checkImportData(excelList);
            // 4.将 userExcelList 转成 userList
            successCount = xnLeadService.saveImportLead(countPO.getNewList(), tenantCode);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ReturnT(200, "导入成功"+successCount+"条,导入失败"+countPO.getErrorCount()+"条");
    }

    /**
     * 校验必填数据信息
     * @param excelList
     * @throws Exception
     */
    private ImportLeadCountPO checkImportData(List<ImportLeadPO> excelList){
        Integer errorCount= 0;
        ImportLeadCountPO po = new ImportLeadCountPO();
        List<ImportLeadPO> newList = new ArrayList<>();
        // 行号从第2行开始
        int rowNo = 2;
        // 遍历校验所有行和列
        for (ImportLeadPO leadPo : excelList) {
            String username = leadPo.getLeadName();
            if (StringUtils.isEmpty(username)) {
                log.info("第{}行线索名称不能为空", rowNo);
                errorCount++;
                continue;
            }
            // 重要程度
            DegreeImportantEnum i = DegreeImportantEnum.getByKey(leadPo.getZhongyaochengdu());
            if (i != null){
                leadPo.setZhongyaochengdu(i.getValue());
            }else {
                errorCount++;
                continue;
            }
            // 线索来源
            LeadSourceEnum l = LeadSourceEnum.getByKey(leadPo.getXiansuolaiyuan());
            if (l != null){
                leadPo.setXiansuolaiyuan(l.getValue());
            }else {
                errorCount++;
                continue;
            }
            // 线索状态
            LeadStatusEnum ls =  LeadStatusEnum.getByKey(leadPo.getGenjinzhuangtai());
            if (ls != null){
                leadPo.setGenjinzhuangtai(ls.getValue());
            }else {
                errorCount++;
                continue;
            }
            // 行业
            IndustryEnum in = IndustryEnum.getValue(leadPo.getIndustry());
            if (in != null){
                leadPo.setIndustry(in.getValue());
            }else {
                errorCount++;
                continue;
            }
            newList.add(leadPo);
        }
        po.setNewList(newList);
        po.setErrorCount(errorCount);
        return po;
    }

    /**
     * 导出
     */
    @RequestMapping("/excel/export")
    public RespBody exportData(HttpServletRequest request, HttpServletResponse response) {
        final ID user = getRequestUser(request);
        Assert.isTrue(
                Application.getPrivilegesManager().allow(user, ZeroEntry.AllowDataExport),
                getLang(request, "NoOpPrivileges"));
        Integer dataRange = getIntParameter(request, "dr", BatchOperatorQuery.DR_PAGED);
//        JSONObject queryData = (JSONObject) ServletUtils.getRequestJson(request);
        JSONObject queryData = new JSONObject();
        queryData.put("pageNo", getIntParameter(request, "pageNo"));
        queryData.put("pageSize", getIntParameter(request, "pageSize"));
        queryData.put("param", getParameter(request, "param"));
        queryData.put("_selected", getParameter(request, "_selected"));
        log.info("参数queryData：{}", queryData);
        String fileName = "线索管理"+DateUtil.now()+".xlsx";
        String sheetName = "线索管理";
        List<XnLeadVO> userList = xnLeadService.selectExportLeadForAdmin(queryData, dataRange);
        List<ImportLeadPO> userExcelList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(userList)){
            for (XnLeadVO vo1 : userList){
                ImportLeadPO leadPO = new ImportLeadPO();
                BeanUtils.copyProperties(vo1, leadPO);
                userExcelList.add(leadPO);
            }
        }
        try {
            ExcelUtils.writeExcel(response, userExcelList, fileName, sheetName, ImportLeadPO.class);
            return RespBody.ok(fileName);
        } catch (Exception e) {
            e.printStackTrace();
            return RespBody.error(e.getLocalizedMessage());
        }
    }


    /*************************************************************** 后台结束 ***************************************************************/
    /*************************************************************** 前台公海开始 ***************************************************************/

    /**
     * 前台-查询公海线索列表
     * @return
     */
    @RequestMapping("/highSea")
    public ModelAndView selectLeadForHighSea(HttpServletRequest request) {
        final ID user = getRequestUser(request);
        String tenantCode = (String) ServletUtils.getSessionAttribute(request, LoginController.TENANT_CODE);
        UserContextHolder.setTenant(tenantCode);
        // TODO 未确定前端HTML
        ModelAndView mv = createModelAndView("/xianniu/admin/lead/lead-highSea");
        mv.getModel().put("entityName", "XnLead");
        mv.getModel().put("entityLabel", "公海线索");
        mv.getModel().put("entityIcon", "entityIcon");
        JSON config = DataListManager.instance.getFieldsLayout("XnLead", user, tenantCode);
        mv.getModel().put("DataListConfig", JSON.toJSONString(config));
        return mv;
    }

    /**
     * 前台-单个删除公海线索
     * @param idList
     * @return
     */
    @PostMapping("/deleteLeadForHighSea")
    public JSONAware deleteLeadForHighSea(@RequestParam String idList, HttpServletRequest request) {
        if (StringUtils.isEmpty(idList)){
            return RespBody.errorl("PlsSet1FieldShowLeast");
        }
        String tenantCode = (String) ServletUtils.getSessionAttribute(request, LoginController.TENANT_CODE);
        UserContextHolder.setTenant(tenantCode);
        log.info("后台-单个删除公海线索:{}", idList);
        ReturnT returnT = xnLeadService.deleteLeadForHighSea(idList);

        if (returnT.getCode() != 200){
            return RespBody.errorl(returnT.getMsg());
        }
        return RespBody.ok();
    }

    /**
     * 前台-单条分派线索
     * @param po
     * @return
     */
    @PostMapping("/assignLeadForHighSea")
    public JSONAware assignLeadForHighSea(@RequestBody AssignLeadPO po, HttpServletRequest request) {
        if (CollectionUtils.isEmpty(po.getIds()) || StringUtils.isEmpty(po.getTo())){
            return RespBody.errorl("PlsSelectSomeToWho", "分派");
        }
        String tenantCode = (String) ServletUtils.getSessionAttribute(request, LoginController.TENANT_CODE);
        UserContextHolder.setTenant(tenantCode);
        log.info("前台-单条分派线索:{}", JSON.toJSON(po));
        ReturnT returnT = xnLeadService.assignLeadForHighSea(po);

        if (returnT.getCode() != 200){
            return RespBody.errorl(returnT.getMsg());
        }
        return RespBody.ok();
    }

    /**
     * 前台-单条领取线索
     * @param po
     * @return
     */
    @PostMapping("/receiveLeadForHighSea")
    public JSONAware receiveLeadForHighSea(@RequestBody AssignLeadPO po, HttpServletRequest request) {
        if (CollectionUtils.isEmpty(po.getIds())){
            return RespBody.errorl("PlsSet1FieldShowLeast");
        }
        String tenantCode = (String) ServletUtils.getSessionAttribute(request, LoginController.TENANT_CODE);
        UserContextHolder.setTenant(tenantCode);
        log.info("前台-单条分派线索:{}", po.getIds());
        ReturnT returnT = xnLeadService.receiveLeadForHighSea(po.getIds());
        if (returnT.getCode() != 200){
            return RespBody.errorl(returnT.getMsg());
        }
        return RespBody.ok();
    }

    /**
     * 前台-新增公海线索
     * @param po
     * @return
     */
    @PostMapping("/addLeadForHighSea")
    public JSONAware addLeadForHighSea(@RequestBody AddXnLeadPO po, HttpServletRequest request){
        if (Objects.isNull(po)){
            return RespBody.errorl("PlsSet1FieldShowLeast");
        }
        String tenantCode = (String) ServletUtils.getSessionAttribute(request, LoginController.TENANT_CODE);
        UserContextHolder.setTenant(tenantCode);
        log.info("前台-新增公海线索:{}", JSON.toJSON(po));
        ID currentUser = (ID) ServletUtils.getSessionAttribute(request, WebUtils.CURRENT_USER);
        po.setCreatedBy(currentUser.toString());
        ReturnT returnT = xnLeadService.addLeadForHighSea(po);
        return JSONUtils.toJSONObject(
                new String[] { "id"},
                new Object[] { returnT.getData()});
    }

    /*************************************************************** 前台公海结束 ***************************************************************/
    /*************************************************************** 前台私海开始 ***************************************************************/

    /**
     * 前台-查询私海线索列表
     * @param request
     * @return
     */
    @RequestMapping("/leadPrivateSea")
    public ModelAndView selectLeadForPrivateSea(HttpServletRequest request) {
        final ID user = getRequestUser(request);
        String tenantCode = (String) ServletUtils.getSessionAttribute(request, LoginController.TENANT_CODE);
        UserContextHolder.setTenant(tenantCode);
        // TODO 未确定前端HTML
        ModelAndView mv = createModelAndView("/xianniu/admin/lead/lead-privateSea", "XnLead", user);
        mv.getModel().put("entityLabel", "私海线索");
        mv.getModel().put("entityIcon", "entityIcon");
        JSON config = DataListManager.instance.getFieldsLayout("XnLead", user, tenantCode);
        mv.getModel().put("DataListConfig", JSON.toJSONString(config));
        return mv;
    }

    /**
     * 前台-新增私海线索
     * @param po
     * @return
     */
    @PostMapping("/addLeadForPrivateSea")
    public JSONAware addLeadForPrivateSea(@RequestBody AddXnLeadPO po, HttpServletRequest request){
        if (Objects.isNull(po)){
            return RespBody.errorl("PlsSet1FieldShowLeast");
        }
        String tenantCode = (String) ServletUtils.getSessionAttribute(request, LoginController.TENANT_CODE);
        UserContextHolder.setTenant(tenantCode);
        log.info("前台-新增私海线索:{}", JSON.toJSON(po));
        ID currentUser = (ID) ServletUtils.getSessionAttribute(request, WebUtils.CURRENT_USER);
        po.setCreatedBy(currentUser.toString());
        ReturnT returnT = xnLeadService.addLeadForPrivateSea(po);
        return JSONUtils.toJSONObject(
                new String[] { "id"},
                new Object[] { returnT.getData()});
    }

    /**
     * 前台-放回线索池
     * @param po
     * @return
     */
    @PostMapping("/putBackLeadForPrivateSea")
    public JSONAware putBackLeadForPrivateSea(@RequestBody PutLeadPO po, HttpServletRequest request) {
        if (ObjectUtil.isEmpty(po)){
            return RespBody.errorl("PlsSet1FieldShowLeast");
        }
        String tenantCode = (String) ServletUtils.getSessionAttribute(request, LoginController.TENANT_CODE);
        UserContextHolder.setTenant(tenantCode);
        log.info("前台-放回线索池:{}", JSON.toJSON(po));
        ReturnT returnT = xnLeadService.putBackLeadForPrivateSea(po);

        if (returnT.getCode() != 200){
            return RespBody.errorl(returnT.getMsg());
        }
        return RespBody.ok();
    }

    /**
     * 强制回收线索到线索池
     * @param po
     * @return
     */
    @PostMapping("/recycleLeadForPrivateSea")
    public JSONAware recycleLeadForPrivateSea(@RequestBody PutLeadPO po, HttpServletRequest request) {
        if (ObjectUtil.isEmpty(po)){
            return RespBody.errorl("PlsSet1FieldShowLeast");
        }
        String tenantCode = (String) ServletUtils.getSessionAttribute(request, LoginController.TENANT_CODE);
        UserContextHolder.setTenant(tenantCode);
        log.info("强制回收线索到线索池:{}", JSON.toJSON(po));
        ReturnT returnT = xnLeadService.recycleLeadForPrivateSea(po);

        if (returnT.getCode() != 200){
            return RespBody.errorl(returnT.getMsg());
        }
        return RespBody.ok(returnT.getMsg());
    }
    /*************************************************************** 前台私海结束 ***************************************************************/


    public JSON buildLeadAdminDataListConfig(){
        List<Map<String, Object>> columnList = new ArrayList<>();
        Map<String, Object> m1 = JSONUtils.toJSONObject(
                new String[]{"field", "type", "label"},
                new Object[]{"leadName", "TEXT", "线索名称"});
        Map<String, Object> m2 = JSONUtils.toJSONObject(
                new String[]{"field", "type", "label"},
                new Object[]{"leadTeam", "TEXT", "所属线索池"});
        Map<String, Object> m3 = JSONUtils.toJSONObject(
                new String[]{"field", "type", "label"},
                new Object[]{"createdBy", "TEXT", "创建人"});
        Map<String, Object> m4 = JSONUtils.toJSONObject(
                new String[]{"field", "type", "label"},
                new Object[]{"createdOn", "TEXT", "创建时间"});
        Map<String, Object> m5 = JSONUtils.toJSONObject(
                new String[]{"field", "type", "label"},
                new Object[]{"receiveBy", "TEXT", "领取人"});
        Map<String, Object> m6 = JSONUtils.toJSONObject(
                new String[]{"field", "type", "label"},
                new Object[]{"receiveOn", "TEXT", "领取时间"});
        Map<String, Object> m7 = JSONUtils.toJSONObject(
                new String[]{"field", "type", "label"},
                new Object[]{"receiveStatus", "TEXT", "领取状态"});
        Map<String, Object> m8 = JSONUtils.toJSONObject(
                new String[]{"field", "type", "label"},
                new Object[]{"genjinzhuangtai", "TEXT", "线索状态"});
        columnList.add(m1);
        columnList.add(m2);
        columnList.add(m3);
        columnList.add(m4);
        columnList.add(m5);
        columnList.add(m6);
        columnList.add(m7);
        columnList.add(m8);
        return JSONUtils.toJSONObject(
                new String[]{"entity", "nameField", "fields"},
                new Object[]{"XnLead", "leadId", columnList});
    }

}
